﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class BlackMarketCollection : BaseCollection<BlackMarket>
	{
		#region Constructor

		public BlackMarketCollection()
		{
		}

		public BlackMarketCollection(SqlDataReader reader)
		{
			Load(reader);
		}

		#endregion

		#region Loading

		public override sealed void Load(SqlDataReader reader)
		{
			if (Count > 0)
			{
				Clear();
			}

			while (reader.Read())
			{
				BlackMarket bm = new BlackMarket();
				bm.Load(reader);

				Add(bm);
			}

			if (!reader.IsClosed)
			{
				reader.Close();
			}

			reader.Dispose();
		}

		#endregion

		#region Filtering

		public BlackMarketCollection Filter(String keywords)
		{
			BlackMarketCollection markets = new BlackMarketCollection();

			keywords = keywords.ToLower();

			foreach (BlackMarket m in this)
			{
				Address a = m.Address;

				if (m.Title.ToLower().Contains(keywords) ||
					a.Postcode.ToLower().Contains(keywords) ||
					a.Location.ToLower().Contains(keywords) ||
					a.City.Title.ToLower().Contains(keywords) ||
					a.City.Country.ToLower().Contains(keywords))
				{
					markets.Add(m);
				}
			}

			return markets;
		}

		#endregion
	}
}